<?php

namespace App\Http\Controllers\Frontend;

use App\Http\Controllers\Controller;
use App\Models\Post;
use App\Services\PostService;
use Illuminate\Http\Request;
use Inertia\Inertia;
use App\Models\Category;

/**
 * 前台文章控制器
 * 
 * 职责：
 * - 处理前台文章相关的HTTP请求
 * - 调用 PostService 处理业务逻辑
 * - 返回视图
 */
class PostController extends Controller
{
    /**
     * 文章服务
     * 
     * @var PostService
     */
    protected PostService $postService;

    /**
     * 构造器
     */
    public function __construct(PostService $postService)
    {
        $this->postService = $postService;
    }

    /**
     * 显示文章详情页
     */
    public function show(Post $post)
    {
        // 权限检查：未发布的文章需要权限才能查看
        if ($post->status !== 'published' && !auth()->user()?->can('view unpublished posts')) {
            abort(404);
        }

        // 先保存 series_id（在调用 getPostDetail 之前）
        $seriesId = $post->series_id;

        // 调用服务层获取文章详情（包含阅读量统计）
        $post = $this->postService->getPostDetail($post);

        // 确保 series_id 被保留（如果 getPostDetail 没有保留）
        if (!$post->series_id && $seriesId) {
            $post->series_id = $seriesId;
        }

        // 调用服务层获取相关文章
        $relatedPosts = $this->postService->getRelatedPosts($post, 3);

        // 调用服务层获取上一篇和下一篇文章
        $prevNextPosts = $this->postService->getPrevNextPosts($post);

        // 获取系列信息和同系列文章
        $seriesPosts = null;
        $seriesInfo = null;
        $prevInSeries = null;
        $nextInSeries = null;
        
        // 使用保存的 series_id 或 post 中的 series_id
        $currentSeriesId = $post->series_id ?? $seriesId;
        
        if ($currentSeriesId) {
            // 确保 post 对象有 series_id（用于后续方法调用）
            if (!$post->series_id) {
                $post->series_id = $currentSeriesId;
            }
            
            // 加载系列信息
            $series = \App\Models\Series::with('author')->find($currentSeriesId);
            if ($series && $series->is_visible) {
                // 直接传递 Series 模型，Inertia.js 会自动序列化
                $seriesInfo = $series;
                
                // 获取同系列文章（只包含已发布的文章）
                $seriesPosts = $this->postService->getSeriesPosts($post);
                
                // 获取系列内上一篇和下一篇文章
                $prevInSeries = $this->postService->getPreviousPost($post);
                $nextInSeries = $this->postService->getNextPost($post);
            }
        }
        
        return Inertia::render('Blog/Show', [
            'post' => $post,
            'relatedPosts' => $relatedPosts,
            'prevPost' => $prevNextPosts['prev'],
            'nextPost' => $prevNextPosts['next'],
            'series' => $seriesInfo,
            'seriesPosts' => $seriesPosts,
            'prevInSeries' => $prevInSeries,
            'nextInSeries' => $nextInSeries,
        ]);
    }

    /**
     * 显示文章预览页面
     */
    public function preview()
    {
        return Inertia::render('Blog/Posts/Preview', [
            'categories' => Category::orderBy('name')->get(),
        ]);
    }
} 